package com.dragon.tools.service.impl;

import com.dragon.tools.pojo.MyRequestBody;
import com.dragon.tools.pojo.MyResponseBody;
import com.dragon.tools.pojo.SqlElement;
import com.dragon.tools.service.GetService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GetServiceImpl implements GetService {

    @Override
    public String generateSql() {
        return null;
    }

    @Override
    public MyResponseBody previewSql(MyRequestBody request, MyResponseBody body) {
        // 创建sql对象
        SqlElement sqle = new SqlElement();
        // 封装sql属性
        sqle.setDatabaseName(request.getDatabaseName());
        sqle.setTabName(request.getTabName());
        sqle.setTabType("INTERIOR".equals(request.getTabType()) ? "" : request.getTabType());
        sqle.setTabComment(request.getTabComment());
        sqle.setColumnList(parseTabColumns(request.getColumnName()));
        sqle.setColumnTypeList(parseTabColumns(request.getColumnType()));
        sqle.setColumnCommList(parseTabColumns(request.getColumnComment()));
        sqle.setParColumn(request.getParColumn());
        sqle.setParType(request.getParType());
        sqle.setParComm(request.getParComm());
        sqle.setDelimited(request.getDelimited());
        sqle.setStoreType(request.getStoreType());
        sqle.setHdfsPath(request.getHdfsPath());

        // 生成sql字符串 并封装到response里
        body.setPreviewSql(sqle.generate());
        return body;
    }

    public List<String> parseTabColumns(String columns){
        ArrayList<String> list = new ArrayList<String>();
        String[] splits = columns.split(",");
        for (String split : splits) {
            list.add(split);
        }
        return list;
    }
}
